< previous page page_485 next page >

Page 485
Table 21.3 Parts of an INI File
ElementDescription
SectionA name enclosed in brackets ([]) that groups a set of values and keys together.
KeyA unique string. The key will be followed by an equal sign (=) and a value. A key needs to be unique only within a specific section.
ValueThe actual information of a particular key in the INI file. A section and key together are used to read or write a value.

The order of sections and keys within the file is not important because the section and key names (should) point to only one value. One final note on the structure of an INI file: A semicolon (;) is used to indicate a comment; any keys or values following a semicolon are ignored. For example, look at this INI section:
[Settings]
DBLocation=P:\USERINFO.MDB
;DBLocation=D:\CODE\TEST.MDB
In the preceding section, switching from a local development database to a production database is easy. You can simply "comment out" the line you don't want.
Using INI Files in Visual Basic
One reason INI files are easy to use is that you do not have to worry about creating the file, opening the file, or finding the correct line. Before you can use INI files, however, you have to declare two Windows API functions and write a couple of "wrapper" functions around them. (To learn more about API calls, see Chapter 20, "Accessing the Windows API.") Simply add a new module to your program, and enter the code from Listing 21.3 in the General Declarations section.
tip.gif
Build a library of useful functions like these in a separate module (for example, UTILITY.BAS) that can easily be added to multiple projects.

Listing 21.3 INIFUNC.BASUsing INI Files in Your Program
 'API DECLARATIONS
Declare Function GetPrivateProfileString Lib "kernel32" Alias _
                 "GetPrivateProfileStringA" (ByVal lpApplicationName _
                 As String, ByVal lpKeyName As Any, ByVal lpDefault _
                 As String, ByVal lpReturnedString As String, ByVal _
                 nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias _
                 "WritePrivateProfileStringA" (ByVal lpApplicationName _
                 As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
                 ByVal lpFileName As String) As Long
continues

 
< previous page page_485 next page >